﻿package base.btn {
	import base.btn.BtnHit;
	import flash.display.Sprite;
	import base.btn.BtnHitEvent;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	import base.dspObj.DoOperations;
	
	public class BtnArrow extends BtnHit {
		
		private var posXInitArrow: Number;
		private var moveXArrow: Number;
		private var countFramesAnimArrow: uint;
		
		public var arrow: Sprite;
		
		public function BtnArrow(moveXArrow: Number = 3, countFramesAnimArrow: uint = 5, arrow: Sprite = null, hit: Sprite = null): void {
			if ((this.arrow == null) && (arrow != null)) {
				this.addChild(arrow);
				this.arrow = arrow;
			}
			super(hit);
			this.posXInitArrow = this.arrow.x;
			this.moveXArrow = moveXArrow;
			this.countFramesAnimArrow = countFramesAnimArrow;
			this.addEventListener(BtnHitEvent.OVER, this.onOverHandler);
			this.addEventListener(BtnHitEvent.OUT, this.onOutHandler);
		}
		
		protected function setElementsOnOutOver(isOutOver: uint): void {
			var onComplete: Function = null;
			if (isOutOver == 1) onComplete = this.animArrow;
			TweenMax.to(this.arrow, Math.ceil(this.countFramesAnimArrow / 2), {x: this.posXInitArrow + [0, this.moveXArrow][isOutOver], useFrames: true, ease: [Quad.easeIn, Quad.easeOut][isOutOver], onComplete: onComplete});
		}
		
		private function animArrow(): void {
			TweenMax.to(this.arrow, this.countFramesAnimArrow, {x: this.posXInitArrow - this.moveXArrow, useFrames: true, ease: Quad.easeIn, yoyo: true, repeat: -1});
		}
		
		protected function hideShow(isHideShow: uint): void {
			DoOperations.hideShow(this, isHideShow);
		}
		
		override public function addMouseEvents(): void {
			super.addMouseEvents();
			this.hideShow(1);
		}
		
		override public function removeMouseEvents(): void {
			super.removeMouseEvents();
			this.hideShow(0);
		}
		
		private function onOverHandler(e: BtnHitEvent): void {
			this.setElementsOnOutOver(1);
		}
		
		private function onOutHandler(e: BtnHitEvent): void {
			this.setElementsOnOutOver(0);
		}
		
	}

}

